package servlets;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import sun.misc.Sort;

/**
 * Servlet implementation class StringSplitServlets
 */
@WebServlet("/StringSplitServlets")
public class StringSplitServlets extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public StringSplitServlets() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();

		List<String> strings = new ArrayList<String>();
		HashSet<String> pieces = new HashSet<String>();
		List<String> strings2 = new ArrayList<String>();

		strings.add("Pix Bic bla bla");
		strings.add("Pix Msg bla bla");
		strings.add("Pix Exa bla bla");
		strings.add("Pix Tem bla bla");
		strings.add("Creion Bic bla bla");
		strings.add("Creion Msg bla bla");
		strings.add("Creion Exa bla bla");
		strings.add("Creion Tem bla bla");

		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 "
				+ "Transitional//EN\">");

		out.println("<html>");

		out.println("<head>");
		out.println("<title>" + "Main page!" + "</title>");
		out.println("</head>");
		out.println("<body bgcolor=\"#F3FBFE\">");

		for (String temp : strings) {
			out.println("<br/>");
			out.println(temp);
		}

		out.println("Dupa split");
		for (String temp : strings) {
			String[] bucati = temp.split(" ");
			pieces.add(bucati[0]);

		}
		out.println("<br/>Categoriile");

		strings2.clear();

		for (String temp : pieces) {
			strings2.add(temp);
		}

		Collections.sort(strings2);

		out.println("<form method=\"get\" >");
		for (String temp : strings2) {
			out.println("<br/><input type=\"submit\" name=\"" + temp
					+ "\" value=\"" + temp + "\">");
		}
		out.println("</form>");

		out.println("<br/>");
		for (String temp : strings2) {
			if (request.getParameter(temp) != null)
				for (String temp2 : strings) {
					String[] s = temp2.split(" ");
					if (s[0].equals(temp)) {
						out.println("<br/>"+temp2);
					}
				}
		}

		out.println("</body>");
		out.println("</html>");

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
